home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / reuse.lha / reuse / m2c / Idents.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  5KB  |  252 lines

  1. #include "SYSTEM_.h"
  2.  
  3. #ifndef DEFINITION_DynArray
  4. #include "DynArray.h"
  5. #endif
  6.  
  7. #ifndef DEFINITION_Strings
  8. #include "Strings.h"
  9. #endif
  10.  
  11. #ifndef DEFINITION_StringMem
  12. #include "StringMem.h"
  13. #endif
  14.  
  15. #ifndef DEFINITION_IO
  16. #include "IO.h"
  17. #endif
  18.  
  19. #ifndef DEFINITION_Strings
  20. #include "Strings.h"
  21. #endif
  22.  
  23. #ifndef DEFINITION_StringMem
  24. #include "StringMem.h"
  25. #endif
  26.  
  27. #ifndef DEFINITION_Idents
  28. #include "Idents.h"
  29. #endif
  30.  
  31. Idents_tIdent Idents_NoIdent;
  32.  
  33. #define cNoIdent    0
  34. #define InitialTableSize    512
  35. #define HashTableSize    256
  36. typedef struct S_1 {
  37.     StringMem_tStringRef String;
  38.     Strings_tStringIndex Length;
  39.     Idents_tIdent Collision;
  40. } IdentTableEntry;
  41. typedef LONGCARD HashIndex;
  42. static struct S_2 {
  43.     IdentTableEntry A[1000000 + 1];
  44. } *TablePtr;
  45. static LONGINT IdentTableSize;
  46. static Idents_tIdent IdentCount;
  47. static struct S_3 {
  48.     Idents_tIdent A[HashTableSize + 1];
  49. } HashTable;
  50. static HashIndex i;
  51.  
  52.  
  53. Idents_tIdent Idents_MakeIdent
  54. # ifdef __STDC__
  55. (Strings_tString *s)
  56. # else
  57. (s)
  58. Strings_tString *s;
  59. # endif
  60. {
  61.   HashIndex HashTableIndex;
  62.   Idents_tIdent CurIdent;
  63.   LONGINT lIdentCount;
  64.  
  65.   {
  66.     register Strings_tString *W_1 = s;
  67.  
  68.     if (W_1->Length == 0) {
  69.       HashTableIndex = 0;
  70.     } else {
  71.       HashTableIndex = (ORD(W_1->Chars.A[1]) + ORD(W_1->Chars.A[W_1->Length]) * 11 + W_1->Length * 26) % HashTableSize;
  72.     }
  73.   }
  74.   CurIdent = HashTable.A[HashTableIndex];
  75.   for (;;) {
  76.     if (CurIdent == cNoIdent) {
  77.       goto EXIT_1;
  78.     }
  79.     {
  80.       register IdentTableEntry *W_2 = &TablePtr->A[CurIdent];
  81.  
  82.       if (W_2->Length == s->Length && StringMem_IsEqual(W_2->String, s)) {
  83.         return CurIdent;
  84.       }
  85.       CurIdent = W_2->Collision;
  86.     }
  87.   } EXIT_1:;
  88.   INC(IdentCount);
  89.   lIdentCount = IdentCount;
  90.   if (lIdentCount == IdentTableSize) {
  91.     DynArray_ExtendArray((ADDRESS *)&TablePtr, &IdentTableSize, (LONGINT)sizeof(IdentTableEntry));
  92.   }
  93.   {
  94.     register IdentTableEntry *W_3 = &TablePtr->A[IdentCount];
  95.  
  96.     W_3->String = StringMem_PutString(s);
  97.     W_3->Length = s->Length;
  98.     W_3->Collision = HashTable.A[HashTableIndex];
  99.   }
  100.   HashTable.A[HashTableIndex] = IdentCount;
  101.   return IdentCount;
  102. }
  103.  
  104. void Idents_GetString
  105. # ifdef __STDC__
  106. (Idents_tIdent i, Strings_tString *s)
  107. # else
  108. (i, s)
  109. Idents_tIdent i;
  110. Strings_tString *s;
  111. # endif
  112. {
  113.   {
  114.     register IdentTableEntry *W_4 = &TablePtr->A[i];
  115.  
  116.     StringMem_GetString(W_4->String, s);
  117.   }
  118. }
  119.  
  120. StringMem_tStringRef Idents_GetStringRef
  121. # ifdef __STDC__
  122. (Idents_tIdent i)
  123. # else
  124. (i)
  125. Idents_tIdent i;
  126. # endif
  127. {
  128.   return TablePtr->A[i].String;
  129. }
  130.  
  131. Idents_tIdent Idents_MaxIdent
  132. # ifdef __STDC__
  133. ()
  134. # else
  135. ()
  136. # endif
  137. {
  138.   return IdentCount;
  139. }
  140.  
  141. void Idents_WriteIdent
  142. # ifdef __STDC__
  143. (IO_tFile f, Idents_tIdent i)
  144. # else
  145. (f, i)
  146. IO_tFile f;
  147. Idents_tIdent i;
  148. # endif
  149. {
  150.   Strings_tString s;
  151.  
  152.   Idents_GetString(i, &s);
  153.   Strings_WriteS(f, &s);
  154. }
  155.  
  156. void Idents_WriteIdents
  157. # ifdef __STDC__
  158. ()
  159. # else
  160. ()
  161. # endif
  162. {
  163.   CARDINAL i;
  164.  
  165.   {
  166.     LONGCARD B_1 = 1, B_2 = IdentCount;
  167.  
  168.     if (B_1 <= B_2)
  169.       for (i = B_1;; i += 1) {
  170.         IO_WriteI((System_tFile)IO_StdOutput, (LONGINT)i, 5L);
  171.         IO_WriteC((System_tFile)IO_StdOutput, ' ');
  172.         Idents_WriteIdent((System_tFile)IO_StdOutput, (SHORTCARD)i);
  173.         IO_WriteNl((System_tFile)IO_StdOutput);
  174.         if (i >= B_2) break;
  175.       }
  176.   }
  177. }
  178.  
  179. void Idents_WriteHashTable
  180. # ifdef __STDC__
  181. ()
  182. # else
  183. ()
  184. # endif
  185. {
  186.   Idents_tIdent CurIdent;
  187.   HashIndex i;
  188.   CARDINAL Count;
  189.  
  190.   for (i = 0; i <= HashTableSize; i += 1) {
  191.     IO_WriteI((System_tFile)IO_StdOutput, (LONGINT)i, 5L);
  192.     Count = 0;
  193.     CurIdent = HashTable.A[i];
  194.     while (CurIdent != cNoIdent) {
  195.       INC(Count);
  196.       CurIdent = TablePtr->A[CurIdent].Collision;
  197.     }
  198.     IO_WriteI((System_tFile)IO_StdOutput, (LONGINT)Count, 5L);
  199.     CurIdent = HashTable.A[i];
  200.     while (CurIdent != cNoIdent) {
  201.       IO_WriteC((System_tFile)IO_StdOutput, ' ');
  202.       Idents_WriteIdent((System_tFile)IO_StdOutput, CurIdent);
  203.       CurIdent = TablePtr->A[CurIdent].Collision;
  204.     }
  205.     IO_WriteNl((System_tFile)IO_StdOutput);
  206.   }
  207.   IO_WriteNl((System_tFile)IO_StdOutput);
  208.   IO_WriteS((System_tFile)IO_StdOutput, (STRING)"Idents =", 8L);
  209.   IO_WriteI((System_tFile)IO_StdOutput, (LONGINT)IdentCount, 5L);
  210.   IO_WriteNl((System_tFile)IO_StdOutput);
  211. }
  212.  
  213. void Idents_InitIdents
  214. # ifdef __STDC__
  215. ()
  216. # else
  217. ()
  218. # endif
  219. {
  220.   Strings_tString String;
  221.  
  222.   for (i = 0; i <= HashTableSize; i += 1) {
  223.     HashTable.A[i] = cNoIdent;
  224.   }
  225.   IdentCount = 0;
  226.   Strings_AssignEmpty(&String);
  227.   Idents_NoIdent = Idents_MakeIdent(&String);
  228. }
  229.  
  230. void BEGIN_Idents()
  231. {
  232.   static BOOLEAN has_been_called = FALSE;
  233.  
  234.   if (!has_been_called) {
  235.     has_been_called = TRUE;
  236.  
  237.     BEGIN_IO();
  238.     BEGIN_Strings();
  239.     BEGIN_StringMem();
  240.     BEGIN_DynArray();
  241.     BEGIN_Strings();
  242.     BEGIN_StringMem();
  243.     BEGIN_IO();
  244.     BEGIN_Strings();
  245.     BEGIN_StringMem();
  246.  
  247.     IdentTableSize = InitialTableSize;
  248.     DynArray_MakeArray((ADDRESS *)&TablePtr, &IdentTableSize, (LONGINT)sizeof(IdentTableEntry));
  249.     Idents_InitIdents();
  250.   }
  251. }
  252.